package com.yh.concert.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.yh.concert.domain.dto.MerchartDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.yh.concert.domain.YhMerchart;
import com.yh.concert.service.IYhMerchartService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商家信息Controller
 *
 * @author yh
 * @date 2025-02-26
 */
@Api(tags="商家信息管理")
@RestController
@RequestMapping("/concert/merchart")
public class YhMerchartController extends BaseController
{
    @Autowired
    private IYhMerchartService yhMerchartService;
    @Autowired
    private SysUserMapper sysUserMapper;


    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询商家信息列表
     */
    @ApiOperation("查询商家信息列表")
    @PreAuthorize("@ss.hasPermi('concert:merchart:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhMerchart yhMerchart)
    {
        startPage();
        // 根据权限获取商家信息
        LoginUser loginUser = getLoginUser();
        if (loginUser.getUser().getUserId() == 1 || sysUserRoleMapper.isSysAdmin(loginUser.getUserId()) == 1){
            yhMerchart.setUserId(null);
        }else {
            yhMerchart.setUserId(loginUser.getUserId());
        }
        List<YhMerchart> list = yhMerchartService.selectYhMerchartList(yhMerchart);
        List<MerchartDto> merchartDtos = new ArrayList<>();
        for (YhMerchart item : list){
            SysUser sysUser = sysUserMapper.selectUserById(item.getUserId());
            MerchartDto merchartDto = new MerchartDto();
            merchartDto.setUsername(sysUser.getUserName());
            BeanUtil.copyProperties(item,merchartDto);
            merchartDtos.add(merchartDto);
        }
        return getDataTable(merchartDtos);
    }

    /**
     * 导出商家信息列表
     */
    @ApiOperation("导出商家信息列表")
    @PreAuthorize("@ss.hasPermi('concert:merchart:export')")
    @Log(title = "商家信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhMerchart yhMerchart)
    {
        List<YhMerchart> list = yhMerchartService.selectYhMerchartList(yhMerchart);
        ExcelUtil<YhMerchart> util = new ExcelUtil<YhMerchart>(YhMerchart.class);
        util.exportExcel(response, list, "商家信息数据");
    }

    /**
     * 获取商家信息详细信息
     */
    @ApiOperation("获取商家信息详细信息")
    @PreAuthorize("@ss.hasPermi('concert:merchart:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        YhMerchart merchart = yhMerchartService.getById(id);
        MerchartDto merchartDto = new MerchartDto();
        BeanUtil.copyProperties(merchart,merchartDto);
        SysUser sysUser = sysUserMapper.selectUserById(merchartDto.getUserId());
        merchartDto.setUsername(sysUser.getUserName());
        return AjaxResult.success(merchartDto);
    }

    /**
     * 新增商家信息
     */
    @ApiOperation("新增商家信息")
    @PreAuthorize("@ss.hasPermi('concert:merchart:add')")
    @Log(title = "商家信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhMerchart yhMerchart)
    {
        LoginUser loginUser = getLoginUser();
        yhMerchart.setUserId(loginUser.getUserId());
        yhMerchart.setStatus(0L);
        return toAjax(yhMerchartService.save(yhMerchart));
    }

    /**
     * 修改商家信息
     */
    @ApiOperation("修改商家信息")
    @PreAuthorize("@ss.hasPermi('concert:merchart:edit')")
    @Log(title = "商家信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhMerchart yhMerchart)
    {
        return toAjax(yhMerchartService.updateById(yhMerchart));
    }

    /**
     * 删除商家信息
     */
    @ApiOperation("删除商家信息")
    @PreAuthorize("@ss.hasPermi('concert:merchart:remove')")
    @Log(title = "商家信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhMerchartService.removeByIds(Arrays.asList(ids)));
    }
    /**
     * 删除商家信息
     */
    @ApiOperation("通过商家申请")
    @PreAuthorize("@ss.hasPermi('concert:merchart:pass')")
    @Log(title = "商家信息", businessType = BusinessType.UPDATE)
    @PostMapping("/pass")
    public AjaxResult pass(@RequestBody YhMerchart yhMerchart)
    {
        logger.info("接受到信息{}",yhMerchart);
        // 获取商家身份列表
        SysUser sysUser = sysUserMapper.selectUserById(yhMerchart.getUserId());
        // 查询当前商家是否拥有商家身份
        int merchert = sysUserRoleMapper.isMerchart(sysUser.getUserId());
        if (merchert == 1){
            throw new RuntimeException("该用户已经是商家了，请勿重复提交");
        }else {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(sysUser.getUserId());
            sysUserRole.setRoleId(100L);
            sysUserRoleMapper.insert(sysUserRole);
        }
        yhMerchart.setStatus(1L);
        return toAjax(yhMerchartService.updateById(yhMerchart));
    }
}
